package com.learn.springlearn.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("mq")
public class QueueController {

    @Autowired
    private RabbitTemplate rabbitTemplate;



    @GetMapping("send")
    public String sendMessageByMQ(String name){

        Map<String,Object> message = new HashMap<>();
        message.put("messageId",UUID.randomUUID().toString());
        String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        message.put("date",date);
        message.put("名字","测试");

        rabbitTemplate.convertAndSend("testExchange","smp",message);
        return name;
    }

    @GetMapping("topic/send")
    public String sendTopicMessageByMQ(){

        Map<String,Object> message = new HashMap<>();
        message.put("messageId",UUID.randomUUID().toString());
        String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        message.put("date",date);
        message.put("主题","测试");

        rabbitTemplate.convertAndSend("topicExchange","topic.cc",message);
        return "发送成功";
    }
}
